package com.example.myapplication

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.app.Dialog
import android.app.ProgressDialog
import android.app.TimePickerDialog
import android.content.DialogInterface
import android.graphics.drawable.Drawable
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.CalendarView
import android.widget.DatePicker
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.TimePicker
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import java.util.Calendar

class MyMainActivity : AppCompatActivity() {

    lateinit private var progressBar: ProgressBar
    //   lateinit var textV: TextView
    lateinit var alertDialogTV: TextView
    lateinit var progressDialogTV: TextView
    lateinit var datePickerDialogTV: TextView
    lateinit var timePickerDialogTV: TextView
    lateinit var fragmentTV: TextView
    private var isPopUp = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setView()
    }
    inline fun < reified T : Enum<T>> printAllValues() {
        println(enumValues<T>().joinToString { it.name })
    }

    fun setView(): Unit{

        progressBar = findViewById<ProgressBar>(R.id.progress_bar)
        alertDialogTV = findViewById<TextView>(R.id.alert_dialog)
        progressDialogTV = findViewById<TextView>(R.id.progress_dialog)
        datePickerDialogTV = findViewById<TextView>(R.id.date_picker_dialog)
        timePickerDialogTV = findViewById<TextView>(R.id.time_picker_dialog)
        fragmentTV = findViewById<TextView>(R.id.fragment)

        progressBar.max = 100
        progressBar.min = 0

        progressBar.progress = 10

        alertDialogTV.setOnClickListener( object: View.OnClickListener{
            override fun onClick(p0: View?) {
                val alert:AlertDialog.Builder = AlertDialog.Builder(this@MyMainActivity)
                alert.setTitle("标题")
                alert.setMessage("message")
                alert.setIcon(R.mipmap.ic_scan_open_door)
                alert.setPositiveButton("Positive",object: DialogInterface.OnClickListener{
                    override fun onClick(p0: DialogInterface?, p1: Int) {
                        val toast = Toast.makeText(this@MyMainActivity,"点击了Positive",Toast.LENGTH_SHORT)
                        toast.show()
                    }
                })
                alert.setNegativeButton("Negative", object: DialogInterface.OnClickListener{
                    override fun onClick(p0: DialogInterface?, p1: Int) {
                        val toast = Toast.makeText(this@MyMainActivity,"点击了Negative",Toast.LENGTH_SHORT)
                        toast.show()
                    }
                })
                //中立
                alert.setNeutralButton("Neutral", object: DialogInterface.OnClickListener{
                    override fun onClick(p0: DialogInterface?, p1: Int) {
                        val toast = Toast.makeText(this@MyMainActivity,"点击了中立",Toast.LENGTH_SHORT)
                        toast.show()
                    }
                })

                val dialog = alert.create()
//
                dialog.setOnShowListener(object: DialogInterface.OnShowListener {
                    override fun onShow(p0: DialogInterface?) {
                        println("setOnShowListener")
                    }
                })
                dialog.setOnDismissListener { println("setOnDismissListener") }
                dialog.show()
                dialog.getButton(Dialog.BUTTON_POSITIVE).isAllCaps = false
            }
        })


        progressDialogTV.setOnClickListener( object: View.OnClickListener{
            override fun onClick(p0: View?) {
                val dialog:AlertDialog.Builder = AlertDialog.Builder(this@MyMainActivity)
                dialog.setTitle("标题")
                dialog.setNegativeButton("取消",object :DialogInterface.OnClickListener{
                    override fun onClick(p0: DialogInterface?, p1: Int) {

                    }
                })
                dialog.setCancelable(false)
                dialog.setView(R.layout.progress_dialog)
                dialog.show()
            }
        })


        datePickerDialogTV.setOnClickListener( object: View.OnClickListener{
            override fun onClick(p0: View?) {
                val datePickerDialog: DatePickerDialog = DatePickerDialog(this@MyMainActivity)
                datePickerDialog.setOnDateSetListener(object: DatePickerDialog.OnDateSetListener{
                    override fun onDateSet(p0: DatePicker?, p1: Int, p2: Int, p3: Int) {
                        println(p1)//年
                        println(p2)//月
                        println(p3)//日
                    }
                })
                datePickerDialog.show()
            }
        })


        timePickerDialogTV.setOnClickListener( object: View.OnClickListener{
            override fun onClick(p0: View?) {

                val lis = object : TimePickerDialog.OnTimeSetListener {
                    override fun onTimeSet(p0: TimePicker?, p1: Int, p2: Int) {
                        println(p1)
                        println(p2)
                    }
                }
                val calendar = Calendar.getInstance()
                val hour = calendar.get(Calendar.HOUR)
                val minute = calendar.get(Calendar.MINUTE)

                val timePickerDialog = TimePickerDialog(this@MyMainActivity,lis,hour,minute,true)
                timePickerDialog.show()
            }
        })

        fragmentTV.setOnClickListener(object :View.OnClickListener{
            override fun onClick(v: View?) {

                val manager: FragmentManager = this@MyMainActivity.supportFragmentManager
                if (isPopUp){
                    val transaction: FragmentTransaction = manager.beginTransaction()
                    transaction
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    transaction.replace(R.id.dialog_content,MyFragment()).commit();
                }else{
                    //快速点击会弹出多个Fragment
                    // val manager: FragmentManager = this@MyMainActivity.supportFragmentManager
                    // MyFragment().show(manager,"tag")

                    val fragment: MyFragment? = manager.findFragmentByTag("tag") as? MyFragment
                    if ( fragment == null ){
                        MyFragment().show(manager,"tag")
                    }else{
                        if (!fragment.isAdded) {
                            Log.e("TAG", "onClick: 2", )
                            fragment.show(manager,"tag")
                        }else{
                            Log.e("TAG", "onClick: 3", )
                        }
                    }
                }
                isPopUp = !isPopUp
            }
        })

    }

}
